package cn.gzjp.modules.wx.service;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
import cn.gzjp.common.config.ConfigUtils;
import cn.gzjp.modules.logis.entity.UserPhone;
import cn.gzjp.modules.wx.entity.WxMpAccount;
import cn.hutool.core.util.StrUtil;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.AbstractMap;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class WxService {

    @Autowired
    private WxMpAccountService wxMpAccountService;

    // Key 为appid
    private AbstractMap<String, WxMaService> wxMaServices;


    public WxService() {
        this.wxMaServices = new ConcurrentHashMap<String, WxMaService>();
    }


    /**
     * 根据公众号，获取wxMpservice
     *
     * @param appId
     * @return
     */
    public WxMaService getWxMaService(String appId) {
        WxMaService wxMaService = this.wxMaServices.get(appId);
        if (wxMaService == null) {
            this.updateMpConfig(appId);
            wxMaService = this.wxMaServices.get(appId);
        }
        return wxMaService;
    }


    /**
     * 更新公众号信息到
     *
     * @param appId
     * @return
     */
    public void updateMpConfig(String appId) {
        WxMpAccount mpAccount = this.wxMpAccountService.get(appId);
        if (mpAccount != null) {
            WxMaInMemoryConfig config = new WxMaInMemoryConfig();
            config.setAppid(mpAccount.getId());
            config.setSecret(mpAccount.getSecret());
            config.setToken(ConfigUtils.getProperty("token"));
            config.setAesKey(ConfigUtils.getProperty("aesKey"));
            config.setMsgDataFormat(ConfigUtils.getProperty("msgDataFormat"));
            WxMaService wxMaService = new WxMaServiceImpl();
            wxMaService.setWxMaConfig(config);
            this.wxMaServices.put(appId, wxMaService);
        }
    }

    public UserPhone getUserPhone(String sessionKey, String encryptedData, String ivStr) {
        return UserPhone.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr));
    }


    public WxPayService getPayService(String appId) {
        WxMpAccount mpAccount = this.wxMpAccountService.get(appId);
        if (mpAccount != null) {
            WxPayConfig payConfig = new WxPayConfig();
            payConfig.setAppId(mpAccount.getId());
            payConfig.setMchId(mpAccount.getMchId());
            payConfig.setMchKey(mpAccount.getMchKey());
            payConfig.setSubAppId("");
            payConfig.setSubMchId("");
            payConfig.setKeyPath(mpAccount.getKeyPath());
            WxPayService payService = new WxPayServiceImpl();
            payService.setConfig(payConfig);
            return payService;
        }

        return null;

    }

    /**
     *
     * @param appId
     * @param openid
     * @param page
     * @param formId
     * @param keyword1
     * @param keyword2
     * @param keyword3
     * @param keyword4
     * @param keyword5  承运人电话
     * @param keyword6  温馨提示
     */
    public void sendTemplateMsg(String appId, String openid, String page,String formId, String keyword1, String keyword2,
                                String keyword3, String keyword4, String keyword5, String keyword6,String keyword7) {
        //String templateId ="vefYtbpyhEpImfs78PC4L1OBWt2D344uPsefUzaikMc";
          String templateId ="MirEiPMpPp806EX2GQxW76R9cwuP596KutDGZWGzqVU";//云浮智慧物流
        try {
            WxMaTemplateMessage message = new WxMaTemplateMessage();
            message.setToUser(openid);
            message.setTemplateId(templateId);
            message.setPage(page);
            message.setFormId(formId);
            WxMaTemplateMessage.Data datum1 = new WxMaTemplateMessage.Data("keyword1", keyword1);
            message.addData(datum1);
            WxMaTemplateMessage.Data datum2 = new WxMaTemplateMessage.Data("keyword2", keyword2);
            message.addData(datum2);
            WxMaTemplateMessage.Data datum3 = new WxMaTemplateMessage.Data("keyword3", keyword3);
            message.addData(datum3);
            WxMaTemplateMessage.Data datum4 = new WxMaTemplateMessage.Data("keyword4", keyword4);
            message.addData(datum4);
            WxMaTemplateMessage.Data datum5 = new WxMaTemplateMessage.Data("keyword5", keyword5);
            message.addData(datum5);
            WxMaTemplateMessage.Data datum6 = new WxMaTemplateMessage.Data("keyword6", keyword6);
            message.addData(datum6);
            WxMaTemplateMessage.Data datum7 = new WxMaTemplateMessage.Data("keyword7", keyword7);
            message.addData(datum7);
            this.getWxMaService(appId).getMsgService().sendTemplateMsg(message);
        } catch (WxErrorException e) {
            e.printStackTrace();

        }
    }


}
